<template>
  <view class="form">
    <input v-model="form.name" placeholder="宠物名字" />
    <input v-model="form.type" placeholder="宠物类型 (狗/猫)" />
    <input v-model="form.breed" placeholder="品种" />
    <picker :range="['未知','公','母']" @change="e=>form.gender=e.detail.value">
      <view>性别：{{ ['未知','公','母'][form.gender] }}</view>
    </picker>
    <input v-model="form.age" type="number" placeholder="年龄(岁)" />
    <input v-model="form.weight" type="digit" placeholder="体重(kg)" />
    <switch checked="{{form.neutered}}" @change="e=>form.neutered=e.detail.value?1:0">绝育</switch>
    <switch checked="{{form.vaccinated}}" @change="e=>form.vaccinated=e.detail.value?1:0">疫苗</switch>
    <textarea v-model="form.remark" placeholder="备注"></textarea>
    <button @click="submit">保存</button>
  </view>
</template>

<script>
import request from '@/utils/request.js'

export default {
  data() {
    return {
      form: {
        userId: uni.getStorageSync('user').userId,
        name: '', type: '', breed: '', gender: 0,
        age: '', weight: '', neutered: 0, vaccinated: 0, remark: ''
      }
    }
  },
  methods: {
    async submit() {
      const res = await request.post('/pet/create', this.form)
      if (res.code === 200) {
        uni.showToast({ title: '添加成功', icon: 'success' })
        uni.navigateBack()
      }
    }
  }
}
</script>
